{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.1 逻辑回归模型算法原理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4.1.1 逻辑回归模型的数学原理**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3deXyU5b3+8c83+wIJS8IWlrCFVQGNuLVugIK24PkdrXLaqq2t1h6tVluLtSvt+Z1qbbVaT5Xaamu1aNFW3EBxQWsLAoIgCWBIWMKWACH7Mst9/kj0pBhgSCY8s1zv12tesz3MXCPJ5c09z3M/5pxDRESiX4LXAUREJDxU6CIiMUKFLiISI1ToIiIxQoUuIhIjkrx645ycHJefn+/V24uIRKU1a9bsd87ldvScZ4Wen5/P6tWrvXp7EZGoZGbbj/ScplxERGKECl1EJEao0EVEYoQKXUQkRqjQRURixDEL3cx+b2YVZvbBEZ43M7vfzErMbL2ZnRL+mCIiciyhjNAfA2Ye5flZwOi2y3XAb7oeS0REjtcx90N3zr1lZvlH2WQO8EfXug7vCjPrZWYDnXN7wpRRRMRzzjma/UGafAGafG3X/gDNviAtgSAt/iDN/kDbdet9X8DhD7a7HQjiCwSZNq4/k4b0CnvGcBxYlAfsbHe/vO2xTxS6mV1H6yieoUOHhuGtRUSOzjlHoy9AVYOPQw0tHGrwcajBR02Tj9omH3VNfmqa/NQ2+alt8tHQEqC+xU9j++vmAI2+QNgy9c9Oi9hCtw4e6/CsGc65BcACgMLCQp1ZQ0Q6LRB0VNQ2sauqkb01TVTWNv/fpa71en9dM1UNPlr8wSO+jhn0S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WJmmeF8g5BOcOEVM1sGDOjgqTtpzd6b1n9OngY8bWYjIvXUd8f4LN8FLjyxiTrnaJ/DOfdc2zZ30vpP/idOZLYwCOlk59HEzHoAzwC3OOdqvM5zvMzsM0CFc26NmZ3ndZ4uSgJOAW5yzq00s18B84Dvh/MNIpZzbvqRnjOzG4Bn2wr8XTML0rrgTeWJync8jvRZzOwkYDjwvplB6zTFe2Y21Tm39wRGDMnR/k4AzOxq4DPAtEj9n+tRhHJC9KhhZsm0lvkTzrlnvc7TSWcDs83sYiANyDKzPznnvuBxrs4oB8qdcx/9S2kRrYUeNtE85fI34AIAMysAUojCldiccxucc/2cc/nOuXxa/9JPicQyPxYzmwl8B5jtnGvwOk8nhHJC9KhgraOD3wHFzrlfep2ns5xzdzjnBrf9blxJ6wnoo7HMafud3mlmY9oemgYUhfM9InqEfgy/B35vZh8ALcDVUTgijDW/BlKBV9v+tTzlOSgAAABiSURBVLHCOfc1byOF7kgnRPc4VmedDXwR2GBm69oe+27b+YHFOzcBT7QNGEqBL4XzxXXov4hIjIjmKRcREWlHhS4iEiNU6CIiMUKFLiISI1ToIiIxQoUuIhIjVOgiIjHifwFSYG/zhVes/wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 补充知识点：Sigmoid函数绘制\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "x = np.linspace(-6, 6)  # 通过linspace()函数生成-6到6的等差数列，默认50个数\n",
    "y = 1.0 / (1.0 + np.exp(-x))  # Sigmoid函数计算公式，exp()函数表示指数函数\n",
    "\n",
    "plt.plot(x,y)  # 画图\n",
    "plt.show()  # 展示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6.        , -5.75510204, -5.51020408, -5.26530612, -5.02040816,\n",
       "       -4.7755102 , -4.53061224, -4.28571429, -4.04081633, -3.79591837,\n",
       "       -3.55102041, -3.30612245, -3.06122449, -2.81632653, -2.57142857,\n",
       "       -2.32653061, -2.08163265, -1.83673469, -1.59183673, -1.34693878,\n",
       "       -1.10204082, -0.85714286, -0.6122449 , -0.36734694, -0.12244898,\n",
       "        0.12244898,  0.36734694,  0.6122449 ,  0.85714286,  1.10204082,\n",
       "        1.34693878,  1.59183673,  1.83673469,  2.08163265,  2.32653061,\n",
       "        2.57142857,  2.81632653,  3.06122449,  3.30612245,  3.55102041,\n",
       "        3.79591837,  4.04081633,  4.28571429,  4.53061224,  4.7755102 ,\n",
       "        5.02040816,  5.26530612,  5.51020408,  5.75510204,  6.        ])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 演示下linespace()函数\n",
    "import numpy as np\n",
    "x = np.linspace(-6, 6)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.718281828459045"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 演示下np.exp()函数\n",
    "x = -1 \n",
    "np.exp(-x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4.1.2 逻辑回归模型的代码实现**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
       "                   intercept_scaling=1, l1_ratio=None, max_iter=100,\n",
       "                   multi_class='auto', n_jobs=None, penalty='l2',\n",
       "                   random_state=None, solver='lbfgs', tol=0.0001, verbose=0,\n",
       "                   warm_start=False)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造数据\n",
    "X = [[1, 0], [5, 1], [6, 4], [4, 2], [3, 2]]\n",
    "y = [0, 1, 1, 0, 0]\n",
    "\n",
    "# 模型训练\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "model = LogisticRegression()\n",
    "model.fit(X, y)  # 如果运行时下面出现FutureWarning警告，不要在意，它只是在告诉你以后模型的官方默认参数会有所调整而已，不是报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果不想看到FutureWarning这样的警告信息，可以在代码最上面加上如下内容\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\n"
     ]
    }
   ],
   "source": [
    "# 模型预测 - 预测单个数据\n",
    "print(model.predict([[2,2]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 1]\n"
     ]
    }
   ],
   "source": [
    "# 模型预测 - 预测多个数据1\n",
    "print(model.predict([[1,1], [2,2], [5, 5]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 1 0 0]\n"
     ]
    }
   ],
   "source": [
    "# 模型预测 - 预测多个数据2\n",
    "print(model.predict([[1, 0], [5, 1], [6, 4], [4, 2], [3, 2]]))  # 因为这里演示的多个数据和X是一样的，所以也可以直接写成model.predict(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到其预测准确度为100%。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4.1.3 逻辑回归模型的深入理解**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.97344854, 0.02655146],\n",
       "       [0.39071972, 0.60928028],\n",
       "       [0.17991028, 0.82008972],\n",
       "       [0.63167893, 0.36832107],\n",
       "       [0.82424527, 0.17575473]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预测概率：左列是分类为0的概率，右列是分类为1的概率\n",
    "y_pred_proba = model.predict_proba(X)\n",
    "y_pred_proba  # 直接打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>分类为0的概率</th>\n",
       "      <th>分类为1的概率</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.973449</td>\n",
       "      <td>0.026551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.390720</td>\n",
       "      <td>0.609280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.179910</td>\n",
       "      <td>0.820090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.631679</td>\n",
       "      <td>0.368321</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.824245</td>\n",
       "      <td>0.175755</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    分类为0的概率   分类为1的概率\n",
       "0  0.973449  0.026551\n",
       "1  0.390720  0.609280\n",
       "2  0.179910  0.820090\n",
       "3  0.631679  0.368321\n",
       "4  0.824245  0.175755"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 另外一种打印概率的方式：通过DataFrame展示，更加好看些\n",
    "import pandas as pd\n",
    "a = pd.DataFrame(y_pred_proba, columns=['分类为0的概率', '分类为1的概率'])  # 2.2.1 通过numpy数组创建DataFrame\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.00595248 0.02223835]]\n",
      "[-4.60771284]\n"
     ]
    }
   ],
   "source": [
    "# 打印系数和截距项\n",
    "print(model.coef_)  # 系数k1与k2\n",
    "print(model.intercept_)  # 截距项k0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.00595248],\n",
       "       [0.02223835]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.coef_.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.02655146]\n",
      "[0.60928028]\n",
      "[0.82008972]\n",
      "[0.36832107]\n",
      "[0.17575473]\n"
     ]
    }
   ],
   "source": [
    "# 如果想批量查看预测概率\n",
    "import numpy as np\n",
    "for i in range(5):  # 这里共有5条数据，所以循环5次\n",
    "    print(1 / (1 + np.exp(-(np.dot(X[i], model.coef_.T) + model.intercept_))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**补充知识点：多分类逻辑回归模型演示**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
       "                   intercept_scaling=1, l1_ratio=None, max_iter=100,\n",
       "                   multi_class='auto', n_jobs=None, penalty='l2',\n",
       "                   random_state=None, solver='lbfgs', tol=0.0001, verbose=0,\n",
       "                   warm_start=False)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造数据，此时y有多个分类\n",
    "X = [[1, 0], [5, 1], [6, 4], [4, 2], [3, 2]]\n",
    "y = [-1, 0, 1, 1, 1]  # 这里有三个分类-1、0、1\n",
    "\n",
    "# 模型训练\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "model = LogisticRegression()\n",
    "model.fit(X, y)  # 如果运行时下面出现FutureWarning警告，不要在意，它只是在告诉你以后模型的官方默认参数会有所调整而已，不是报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1]\n"
     ]
    }
   ],
   "source": [
    "print(model.predict([[0, 0]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1,  0,  1,  1,  1])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.88352311 0.02340026 0.09307662]]\n"
     ]
    }
   ],
   "source": [
    "print(model.predict_proba([[0, 0]]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
